const BaseModel = require("./baseModel");

class UserModel extends BaseModel {
  constructor({
    id = null,
    username = null,
    password = null,
    createBy = null,
    createDate = null,
    updateBy = null,
    updateDate = null,
    isDelete = null,
    name = null,
    sex = null,
    deptId = null,
  } = {}) {
    super("users", {
      id,
      username,
      password,
      createBy,
      createDate,
      updateBy,
      updateDate,
      isDelete,
      name,
      sex,
      deptId,
    });
    this.id = id;
    this.username = username;
    this.password = password;
    this.createBy = createBy;
    this.createDate = createDate;
    this.updateBy = updateBy;
    this.updateDate = updateDate;
    this.isDelete = isDelete;
    this.name = name;
    this.sex = sex;
    this.deptId = deptId;
  }

  async getUserByUsername(username) {
    const sql = `SELECT * FROM users WHERE username = ?`;
    const [rows] = await this.pool.query(sql, [username]);
    return rows.length > 0 ? new this.constructor(rows[0]) : null;
  }
}

module.exports = UserModel;
